package com.bristua.mall.order.biz;

import com.bristua.mall.common.bean.PageBean;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.JsonUtil;
import com.bristua.mall.order.dao.TOrderGoodsMapper;
import com.bristua.mall.order.dao.TOrderMapper;
import com.bristua.mall.order.model.TOrder;
import com.bristua.mall.order.model.TOrderGoods;
import com.bristua.mall.order.model.condition.QueryOrder.QueryOrder;
import com.bristua.mall.order.model.condition.TOrderExample;
import com.bristua.mall.order.model.condition.TOrderGoodsExample;
import com.bristua.mall.order.model.vo.QueryOrderVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/24.
 */
@Service
public class QueryOrderBiz {
    @Resource
    private TOrderMapper tOrderMapper;

    @Resource
    private TOrderGoodsMapper tOrderGoodsMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public PageBean queryOrder(QueryOrder queryOrder){
        Map map = JsonUtil.json2Map(JsonUtil.toJson(cacheUtil.getUserSession()));
        String userId = map.get("userId")+"";
        TOrderExample tOrderExample = new TOrderExample();
        tOrderExample.setOrderByClause("create_time desc");
        TOrderExample.Criteria criteria = tOrderExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        String orderType = queryOrder.getOrderType();
        switch (orderType){//1:待付款，2:待发货，3:待收货，4:待评价,0:全部
            case "1":
                criteria.andPayStausEqualTo("1").andOrderStatusEqualTo("1");
                break;
            case "2":
                criteria.andPayStausEqualTo("2");
                criteria.andSendStatusEqualTo("1");
                break;
            case "3":
                criteria.andPayStausEqualTo("2");
                criteria.andSendStatusEqualTo("2");
                criteria.andConfirmStatusEqualTo("1");
                break;
            case "4":
                criteria.andPayStausEqualTo("2");
                criteria.andSendStatusEqualTo("2");
                criteria.andConfirmStatusEqualTo("2");
                criteria.andEvaluateStatusEqualTo("0");
                break;
        }
        Page<TOrder> objects = PageHelper.startPage(queryOrder.getPageNo(),queryOrder.getPageSize());
        List<TOrder> tOrders = tOrderMapper.selectByExample(tOrderExample);
        PageBean data = new PageBean<>(objects);
        TOrderGoodsExample tOrderGoodsExample = new TOrderGoodsExample();
        if(data.getList()!=null && data.getList().size()>0){
            List<QueryOrderVO> queryOrderVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TOrder tOrder = (TOrder) data.getList().get(i);
                QueryOrderVO queryOrderVO = new QueryOrderVO();
                queryOrderVO.setOrderId(tOrder.getOrderId());
                queryOrderVO.setBugNum(tOrder.getBuyNum());
                queryOrderVO.setBuyAllPrice(tOrder.getOrderPrice());
                tOrderGoodsExample.createCriteria().andOrderIdEqualTo(tOrder.getOrderId());
                List<TOrderGoods> tOrderGoods = tOrderGoodsMapper.selectByExampleWithBLOBs(tOrderGoodsExample);
                if(tOrderGoods!=null && tOrderGoods.size()>0){
                    for(TOrderGoods tOrderGoods1 : tOrderGoods){
                        Map<String,Object> orderGoods = new HashMap();
                        orderGoods.put("goodsName",tOrderGoods1.getGoodsName());
                        orderGoods.put("goodsImgUrl",tOrderGoods1.getGoodsImgUrl());
                        orderGoods.put("buyNum",tOrderGoods1.getBuyNum());
                        orderGoods.put("goodsPrice",tOrderGoods1.getGoodsPrice());
                        orderGoods.put("goodsId",tOrderGoods1.getGoodsId());
                        orderGoods.put("productId",tOrderGoods1.getProductId());
                        queryOrderVO.getGoodsList().add(orderGoods);
                    }
                }
                switch (orderType){//1:待付款，2:待发货，3:待收货，4:待评价,0:全部
                    case "1":
                        queryOrderVO.setStatus(1);
                        break;
                    case "2":
                        queryOrderVO.setStatus(2);
                        break;
                    case "3":
                        queryOrderVO.setStatus(3);
                        break;
                    case "4":
                        queryOrderVO.setStatus(4);
                        break;
                    case "0":
                        if("1".equals(tOrder.getPayStaus()) && "1".equals(tOrder.getOrderStatus())){
                            queryOrderVO.setStatus(1);
                        }

                        if("2".equals(tOrder.getPayStaus()) && "1".equals(tOrder.getSendStatus())){
                            queryOrderVO.setStatus(2);
                        }

                        if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "1".equals(tOrder.getConfirmStatus()) ){
                            queryOrderVO.setStatus(3);
                        }

                        if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "2".equals(tOrder.getConfirmStatus())&&"0".equals(tOrder.getEvaluateStatus())){
                            queryOrderVO.setStatus(4);
                        }

                        if("2".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getSendStatus()) && "2".equals(tOrder.getConfirmStatus())&&"1".equals(tOrder.getEvaluateStatus())){
                            queryOrderVO.setStatus(5);
                        }

                        if("1".equals(tOrder.getPayStaus()) && "2".equals(tOrder.getOrderStatus())){
                            queryOrderVO.setStatus(6);
                        }
                        break;
                }


                queryOrderVOS.add(queryOrderVO);
//                tDevice.setIsOnline(DeviceCode.getValueByCode(Integer.valueOf(tDevice.getIsOnline())));
            }
            data.getList().clear();
            data.getList().addAll(queryOrderVOS);
        }
        return data;
    }
}
